const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    touxiang: '../../images/2.jpg',
    items: [{
        name: '男',
        checked: true
      },
      {
        name: '女',
        checked: false
      },
      {
        name: '保密',
        checked: false
      }
    ],
    hostUrl: app.globalData.hostUrl,
    email: "",
    phone: "",

    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部'

  },
  // 手机验证部分

  inputPhoneNum: function(e) {

    let phoneNumber = e.detail.value

    if (phoneNumber.length === 11) {

      let checkedNum = this.checkPhoneNum(phoneNumber)

    }
  },
  checkPhoneNum: function(phoneNumber) {

    let str = /^1\d{10}$/

    if (str.test(phoneNumber)) {

      return true

    } else {

      wx.showToast({

        title: '手机号不正确',

        image: '../../images/cha.png'

      })

      return false

    }

  },
  // 邮箱验证部分

  inputemail: function(e) {

    let email = e.detail.value

    let checkedNum = this.checkEmail(email)

  },

  checkEmail: function(email) {

    let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/

    if (str.test(email)) {

      return true

    } else {

      wx.showToast({

        title: '邮箱格式错误',

        image: '../../images/cha.png'

      })

      return false

    }

  },

  // 地区选择
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      ProvId:null
    })
  },
  //上传图片
  upload: function (e) {
    var that = this;
    wx.chooseImage({
      // count: 9 - that.data.uploaderNum, // 默认9
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
          // console.log(tempFilePaths[i]);
          that.upload_server(tempFilePaths[0]);
          // file.push(res.tempFilePaths[i]);

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUser();
  },
  getUser: function () {//读取会员
    var that = this;
    var userid = wx.getStorageSync('userId');
    wx.request({
      url: app.globalData.apiUrl + 'user/getUser',
      data: {
        // opt: 'getUser',
        userId: userid,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res, '用户信息')
        if (res.data.is_login == 1) {
          
          that.setData({
            user: res.data,
            canIUse: false,
            ProvId: res.data.ProvId,
            zongzichan: parseInt(res.data.Scores) + parseInt(res.data.Commission),
            Scores: res.data.Scores,
            Commission: res.data.Commission,
            
          })
          console.log(that.data.ProvId,'省事')
        } else {
          that.setData({
            canIUse: wx.canIUse('button.open-type.getUserInfo'),
          })
        }
      }

    })
    console.log(that.data.canIUse)
  },
  //上传服务器
  upload_server: function (data) {
    var that = this;
    that.setData({
      file: [],
    })
    wx.uploadFile({
      url: app.globalData.apiUrl + 'Shop/upimg', // 仅为示例，非真实的接口地址
      filePath: data,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        if (res.data !== "") {
          // var back_push=that.data.file;
          // console.log()
          that.setData({
            file: res.data,
          })
        }
        console.log(that.data.file, '返回图片长度')
      }
    })
  },
  //性别选择
  changeSex:function(e){
    this.setData({
      Sex:e.detail.value,
      
    })
  },
  formSubmit: function (e) {
    console.log(e)
    var that = this;
    var formData = e.detail.value;
    formData['Photo'] = that.data.file
    wx.request({
      url: app.globalData.apiUrl + 'user/edit_do',
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data != null) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          that.getUser();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})